// pages/edit/index.js
const App = getApp()
// let reg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1})|(19[0-9]{1}))+\d{8})$/;
let reg = /^1[0-9]{10}$/;
import Toast from '../../weapp/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: '',
    submit_type: '',
    show: false,
    columns: [],
    sex: '',
    standby_phones: [],
    textarea_obj: {
      maxHeight: 300
    },
    form: {
      customer_id: '',
      name: '',
      gender: '',
      level: '',
      mobile: '',
      mobile2: '',
      mobile3: '',
      weixin: '',
      requirement: [],
      location: '',
      intention: '',
      tags: '',
      budget1: '',
      budget2: '',
      demo: ''
    },
    add_list: 5,
    checkbox_list: [
      { title: '住宅', status: false },
      { title: '公寓', status: false },
      { title: '写字楼', status: false },
      { title: '商铺', status: false },
      { title: '别墅', status: false },
      { title: '自建房', status: false },
      { title: '大平层', status: false },
      { title: '车库', status: false },
      { title: '投资', status: false },
      { title: '自住', status: false },
      { title: '其他', status: false },
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    options.type == 'edit' ? this.getCustomerEditInfo(options.id) : ''

    that.setData({
      submit_type: options.type,
      ...this.data.form,
      'form.customer_id': options.id
    })
    // 0CAB96F4-EBD8-528C-1610-9238FC39A928
  },
  addPhone() {
    let obj = {
      value: ''
    }
    // if (this.data.standby_phones.length != 1) {
    this.data.standby_phones.push(obj)
    // }
    this.setData({
      standby_phones: this.data.standby_phones
    })
  },
  handleReduceInput(e) {
    let { index } = e.currentTarget.dataset
    this.data.standby_phones.splice(index, 1)
    this.setData({
      standby_phones: this.data.standby_phones,
      ...this.data.form,
      'form.mobile3': ''
    })
  },
  handleInput(e) {
    let { type } = e.currentTarget.dataset
    this.setData({
      show: true,
      columns: type == 'gender' ? ['男', '女'] : ['A(购买意向极强)', 'B(购买意向较为积极)', 'C(购买意向中低)', 'D(购买意向低)', 'T(同行)', 'K(空号)', 'W(无人接听)'],
      type
    })
  },
  onCancel() {
    this.setData({
      show: false
    })
  },
  onConfirm(e) {
    let type = e.target.dataset.type
    console.log(e)
    this.setData({
      sex: type === 'gender' ? e.detail.value : this.data.sex,
      form: {
        ...this.data.form,
        [type]: type === 'gender' ? e.detail.index + 1 : e.detail.value.substr(0, 1)
      },
      show: false
    })
  },
  onChange(event) {
    console.log(event)
    let index = event.currentTarget.dataset.index
    let arr = this.data.checkbox_list
    arr[index].status = event.detail
    const new_arr = arr.filter(item => {
      return item.status === true
    })
    const map_arr = new_arr.map(item => {
      return item.title
    })
    console.log(this.data.checkbox_list, new_arr)
    this.setData({
      checkbox_list: arr,
      ...this.data.form,
      'form.requirement': map_arr
    });
  },

  onInputChange(e) {
    let { type } = e.currentTarget.dataset
    console.log(e.detail)
    if (type == 'budget1' || type == 'budget2') {
      this.setData({
        form: {
          ...this.data.form,
          [type]: e.detail.value
        }
      })
    } else {
      if (e.detail) {
        this.setData({
          form: {
            ...this.data.form,
            [type]: e.detail || value
          }
        })
      } else {
        this.setData({
          form: {
            ...this.data.form,
            [type]: ''
          }
        })
      }
    }
  },

  handleSaveForm() {
    console.log(this.data.form)
    let _this = this
    let { location, intention, tags, requirement, mobile } = _this.data.form
    if (mobile == '') {
      Toast.fail('手机号不能为空')
      return false
    }
    if (!reg.test(mobile)) {
      Toast.fail('手机号不符合要求');
      return false;
    }
    if (location != '') {
      location = JSON.stringify(location.split())
    }

    if (intention != '') {
      intention = JSON.stringify(intention.split())
    }
    if (requirement != '') {
      requirement = JSON.stringify(requirement)
    }
    console.log(tags);
    if (!(tags == '' || tags == null)) {
      tags = JSON.stringify(tags.split())
    }
    if (tags == null) {
      console.log(4343);
      tags = '';
    }
    let submit_form = Object.assign({}, _this.data.form)
    submit_form.location = location
    submit_form.intention = intention
    submit_form.tags = tags
    submit_form.requirement = requirement


    _this.data.submit_type === 'add' ? delete submit_form.customer_id : null
    console.log(_this.data.submit_type, submit_form)
    App._post('customer/addCustomerInfo', submit_form, function (result) {
      if (result.code === 0) {
        Toast.success('保存成功！');
        Toast({
          type: 'success',
          message: '保存成功!',
          onClose: () => {
            // wx.switchTab({
            //   url: '../social/index',
            // })
            wx.navigateBack({//返回
              delta: 1
            })
          },
        });
      } else {
        Toast.fail(result.msg);
      }
    })
  },
  //获取客户编辑信息
  getCustomerEditInfo(id) {
    let _this = this
    App._post_form('Customer/getCustomerData', {
      c_id: id
    }, function (result) {
      let { id, requirement, mobile3, location, intention, tags, gender } = result.data.customer_data
      mobile3 == '' ? null : _this.data.standby_phones.push({ value: mobile3 })
      if (requirement != null) {
        _this.data.checkbox_list.forEach((item, index) => {
          requirement.forEach((val, key) => {
            if (item.title === val) {
              item.status = true
            }
          });
        });
      }
      if (location != '' && location != null) {
        location = location.join()
      }
      if (intention != '' && intention != null) {
        intention = intention.join()
      }
      if (tags != null && tags != '') {
        tags = tags.join()
      }
      let submit_form = Object.assign({}, result.data.customer_data)
      submit_form.location = location
      submit_form.intention = intention
      submit_form.tags = tags
      submit_form.customer_id = id
      delete submit_form.id
      switch (gender) {
        case 1:
          _this.data.sex = '男'
          break;
        case 2:
          _this.data.sex = '女'
          break;
        default:
          break;
      }
      _this.setData({
        sex: _this.data.sex,
        form: submit_form,
        checkbox_list: _this.data.checkbox_list,
        standby_phones: _this.data.standby_phones
      })
      console.log(_this.data.form)
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})